package com.hfzy.ihk.web.linkcall.job.common;

import com.hfzy.ihk.common.util.Utils;
import com.hfzy.ihk.common.util.uuid.base64uuid.UUIDs;
import com.hfzy.ihk.facade.linkcall.entity.flow.config.SpeakingPackage;
import com.hfzy.ihk.facade.linkcall.entity.knowledge.KnowledgeBase;
import com.hfzy.ihk.facade.linkcall.enums.AffectType;
import com.hfzy.ihk.facade.linkcall.enums.Bool;
import com.hfzy.ihk.facade.linkcall.enums.KnowledgeType;
import com.hfzy.ihk.facade.linkcall.vo.KnowledgeBaseResultVo;
import com.hfzy.ihk.web.linkcall.biz.KnowledgeBase.KnowledgeBaseBiz;
import com.hfzy.ihk.web.linkcall.biz.flow.config.SpeakingPackageBiz;
import com.hfzy.ihk.web.linkcall.constant.RedisKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

/*
* job已跑完暂不执行
* */
//@Component
public class FixKnowledgeBaseResultVoJob {
    private static final Logger logger = LoggerFactory.getLogger(FixKnowledgeBaseResultVoJob.class);

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    KnowledgeBaseBiz knowledgeBaseBiz;
    @Autowired
    SpeakingPackageBiz speakingPackageBiz;
    /**
     * 每2min 执行一次
     * */
//    @Scheduled(cron = "0 0/2 * * * ?")
    public  void fixJob(){
        logger.info("执行job修复知识库的单句问答的对象 start "+ Utils.date2DateTimeString(new Date()));
        int count = 0;//完成条数
        try{
            Boolean jobStatus = (Boolean) redisTemplate.opsForValue().get(RedisKey.getJobStatusRedisKey("FixKnowledgeBaseResultVoJob"));
            logger.info("执行job修复知识库的单句问答的对象 jobStatus 状态 ="+ jobStatus);
            if(jobStatus!=null && jobStatus==true){
                return;
            }
            //设置状态未执行中
            redisTemplate.opsForValue().set(RedisKey.getJobStatusRedisKey("FixKnowledgeBaseResultVoJob"),true);
            boolean flag = true;
            /*List<SpeakingPackage> speakingPackages = speakingPackageBiz.getAllSpeakingPackage();
            for(int i=0;i<speakingPackages.size();i++){
                flag = true;
                int pageSize = 1;
                while (flag) {
                    Map<String, String> params = new HashMap<>();
                    params.put("page", pageSize+"");
                    params.put("pageSize", (pageSize-1)*1000+1000+"");
                    params.put("packageId", speakingPackages.get(i).getId());
                    Page<KnowledgeBase> knowledgeBasePage = knowledgeBaseBiz.getKnowledgeBaseByPage(params);
                    if(knowledgeBasePage!=null && !knowledgeBasePage.getContent().isEmpty()){
                        for(KnowledgeBase knowledgeBase:knowledgeBasePage.getContent()) {
                            knowledgeBase.setAffectType(AffectType.PRIVATE);
                        }
                        count += knowledgeBasePage.getContent().size();
                        knowledgeBaseBiz.updateBatchById(knowledgeBasePage.getContent());
                    }else {
                        flag = false;
                    }
                    pageSize++;
                }
            }*/
            //根据条件获取指定的数据
            while (flag){
                Map<String,String> params = new HashMap<>();
                params.put("page","1");
                params.put("pageSize","1000");
                params.put("notExistsResultVo","");
                Page<KnowledgeBase> knowledgeBasePage =  knowledgeBaseBiz.getKnowledgeBaseByPage(params);
                if(knowledgeBasePage!=null && !knowledgeBasePage.getContent().isEmpty()){
                    for(KnowledgeBase knowledgeBase:knowledgeBasePage.getContent()){
                        List<String> resultList = knowledgeBase.getOneToOneResult();
                        List<KnowledgeBaseResultVo> knowledgeBaseResultVos = new ArrayList<>();
                        if(resultList!=null && !resultList.isEmpty()){
                            resultList.forEach(it->{
                                KnowledgeBaseResultVo knowledgeBaseResultVo = new KnowledgeBaseResultVo();
                                knowledgeBaseResultVo.setRecordFileUrl("");
                                knowledgeBaseResultVo.setId(UUIDs.base64UUID());
                                knowledgeBaseResultVo.setContent(it);
                                knowledgeBaseResultVos.add(knowledgeBaseResultVo);
                            });
                            knowledgeBase.setKnowledgeBaseResultVos(knowledgeBaseResultVos);
                        }
                    }
                    knowledgeBaseBiz.updateBatchById(knowledgeBasePage.getContent());
                }else{
                    flag = false;
                }
            }
        }catch (Exception e){
            logger.error("执行job修复知识库的单句问答的对象",e);
            e.printStackTrace();
        }finally {
            //最终释放，执行完成状态
            redisTemplate.opsForValue().set(RedisKey.getJobStatusRedisKey("FixKnowledgeBaseResultVoJob"),false);
            logger.info("执行job修复知识库的单句问答的对象 end,修复条数 "+count +"。完成时间:"+Utils.date2DateTimeString(new Date()));
        }
    }
}
